package com.net.shop.car.manager;

import java.util.UUID;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.net.shop.car.manager.api.model.Member;
import com.net.shop.car.manager.api.volley.VolleyCenter;
import com.net.shop.car.manager.api.volley.VolleyListenerImpl;
import com.net.shop.car.manager.api.volley.request.Login;
import com.net.shop.car.manager.api.volley.response.LoginResponse;
import com.net.shop.car.manager.thirdparty.volley.BitmapCache;
import com.net.shop.car.manager.utils.LogUtils;

public class App extends Application {

	private static App instance;
	public RequestQueue requestQueue;
	public ImageLoader imageLoader;
	public BitmapCache bitmapCache;

	public String uuid;
	public String mobile;
	/**
	 * 
	 */
	private Member user;
	
	/**
	 * 需要登录
	 */
	public boolean needLogin;

	@Override
	public void onCreate() {
		super.onCreate();

		instance = this;

		VolleyCenter.getVolley().init(this);

		requestQueue = Volley.newRequestQueue(instance);
		bitmapCache = new BitmapCache();
		imageLoader = new ImageLoader(requestQueue, bitmapCache);

		TelephonyManager tm = (TelephonyManager) instance
				.getSystemService(Context.TELEPHONY_SERVICE);
		uuid = UUID.nameUUIDFromBytes(tm.getDeviceId().getBytes()).toString();
		mobile = tm.getLine1Number();
		
		String userAccount = getFromSharedPreferences(Constant.sp.login_name);
		if (!TextUtils.isEmpty(userAccount)) {
			user = new Member();
			user.setMobile(userAccount);
			user.setMemberId(getFromSharedPreferences(Constant.sp.user_id));
			long vb = 0;
			try {
				vb = Long.parseLong(getFromSharedPreferences(Constant.sp.user_vb));
			} catch (NumberFormatException e) {
				LogUtils.e(e.getMessage());
			}
			user.setVb(vb);
			user.setPicUrl(getFromSharedPreferences(Constant.sp.user_head_icon));
			user.setName(getFromSharedPreferences(Constant.sp.username));
		}
		
	}

	public void showToast(String msg) {
		Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
	}

	public void showToastLong(String msg) {
		Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
	}

	public interface OnLoginListener {
		void onLogged();

		void onLoggFailed(String errorMsg);
	}
	
	/**
	 * 
	 */
	public void logout() {
		user = null;
		saveToSharedPreferences(Constant.sp.login_name,
				null);
		saveToSharedPreferences(Constant.sp.password,
				null);

		saveToSharedPreferences(Constant.sp.user_id,
				null);
		saveToSharedPreferences(Constant.sp.username,
				null);
		saveToSharedPreferences(Constant.sp.user_head_icon,
				null);
		saveToSharedPreferences(Constant.sp.user_vb,
				null);
		needLogin = true;
	}

	/**
	 * @param account
	 * @param password
	 * @param androidUserId
	 * @param channelId
	 * @param loginListener
	 */
	public void login(final String account, final String password,
			String androidUserId, String channelId,
			final OnLoginListener loginListener) {
		VolleyCenter.getVolley().addPostRequest(
				new Login(account, password, androidUserId, channelId),
				new VolleyListenerImpl<LoginResponse>(new LoginResponse()) {

					@Override
					public void deleveryResponse(LoginResponse response) {
						if (response.isSuccess()) {
							user = response.getUser();
							saveToSharedPreferences(Constant.sp.login_name,
									account);
							saveToSharedPreferences(Constant.sp.password,
									password);

							saveToSharedPreferences(Constant.sp.user_id,
									user.getMemberId());
							saveToSharedPreferences(
									Constant.sp.login_automatic, true);
							saveToSharedPreferences(Constant.sp.username,
									user.getName());
							saveToSharedPreferences(Constant.sp.user_head_icon,
									user.getPicUrl());
							saveToSharedPreferences(Constant.sp.user_vb,
									user.getVb());
							loginListener.onLogged();
							needLogin = false;
						} else {
							loginListener.onLoggFailed(response.getErrorMsg());
						}
					}
				});
	}

	/** 保存Object到sp */
	public void saveToSharedPreferences(String key, Object value) {
		SharedPreferences sp = getSharedPreferences(
				getString(R.string.app_name), Context.MODE_PRIVATE);
		sp.edit().putString(key, value.toString()).commit();
	}

	/** 从sp获取object */
	public String getFromSharedPreferences(String key) {
		SharedPreferences sp = getSharedPreferences(
				getString(R.string.app_name), Context.MODE_PRIVATE);
		return sp.getString(key, null);
	}

	/** 保存String到sp */
	public void saveToSharedPreferences(String key, String value) {
		SharedPreferences sp = getSharedPreferences(
				getString(R.string.app_name), Context.MODE_PRIVATE);
		sp.edit().putString(key, value).commit();
	}

	/** 从sp获取object */
	public String getStringFromSharedPreferences(String key) {
		SharedPreferences sp = getSharedPreferences(
				getString(R.string.app_name), Context.MODE_PRIVATE);
		return sp.getString(key, null);
	}

	/** 保存boolean到sp */
	public void saveToSharedPreferences(String key, boolean value) {
		SharedPreferences sp = getSharedPreferences(
				getString(R.string.app_name), Context.MODE_PRIVATE);
		sp.edit().putBoolean(key, value).commit();
	}

	/** 从sp获取object */
	public boolean getBooleanFromSharedPreferences(String key) {
		SharedPreferences sp = getSharedPreferences(
				getString(R.string.app_name), Context.MODE_PRIVATE);
		return sp.getBoolean(key, false);
	}

	public void showToast(int textId) {
		showToast(getString(textId));
	}

	public static synchronized App i() {
		return instance;
	}
	
	public Member getUser() {
		if(user == null) {
			needLogin = true;
		}
		return user;
	}

	@Override
	public void onTerminate() {
		super.onTerminate();
	}

	@Override
	public void onLowMemory() {
		super.onLowMemory();
	}
}
